package com.huishu.order.controller;


import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.huishu.commons.config.cache.RedisUtil;
import com.huishu.commons.constant.RedisKeyPrefix;
import com.huishu.commons.pojo.Result;
import com.huishu.order.pojo.dto.CartItemAddDTO;
import com.huishu.order.pojo.dto.NumberHandleDTO;
import com.huishu.order.pojo.dto.OrderSelectDTO;
import com.huishu.order.pojo.dto.UpdateSpecificationsDTO;
import com.huishu.order.pojo.model.CartItemDO;
import com.huishu.order.service.CartItemService;
import com.huishu.order.utils.AssistantContext;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author generator@ljj
 * @since 2023-04-10
 */
@RestController
@RequestMapping("/cart-item")
@Slf4j
public class CartItemController {

    @Autowired
    private CartItemService cartItemService;

    @Autowired
    private RedisUtil redisUtil;

    @PostMapping("/add")
    public Result add(@RequestBody CartItemAddDTO cartItemAddDTO) {
        log.info("添加购物车:{}", JSON.toJSONString(cartItemAddDTO));
        return cartItemService.add(cartItemAddDTO);
    }

    @PostMapping("/list")
    public Result list() {
        return cartItemService.findCartItemList(AssistantContext.getCurrentUid());
    }

    @DeleteMapping("/delete/{id}")
    public Result delete(@PathVariable Long id) {
        LambdaUpdateWrapper<CartItemDO> uw = Wrappers.lambdaUpdate(CartItemDO.class)
                .set(CartItemDO::getIsDelete, 1)
                .eq(CartItemDO::getId, id);
        cartItemService.update(uw);
        redisUtil.decr(RedisKeyPrefix.CARTITEM_COUNT + AssistantContext.getCurrentUid(), 1);
        return Result.success(cartItemService.cartItemCount(AssistantContext.getCurrentUid()));
    }

    @GetMapping("/clear")
    public Result delete() {
        LambdaUpdateWrapper<CartItemDO> uw = Wrappers.lambdaUpdate(CartItemDO.class)
                .set(CartItemDO::getIsDelete, 1)
                .eq(CartItemDO::getUserId, AssistantContext.getCurrentUid());
        cartItemService.update(uw);
        redisUtil.del(RedisKeyPrefix.CARTITEM_COUNT + AssistantContext.getCurrentUid());
        return Result.success(0);
    }

    @PostMapping("/number-handle")
    public Result numberHandle(@RequestBody NumberHandleDTO numberHandleDTO) {
        return cartItemService.numberHandle(numberHandleDTO);
    }

    @PostMapping("/select")
    public Result select(@RequestBody OrderSelectDTO orderSelectDTO) {
        return cartItemService.select(orderSelectDTO);
    }

    @PostMapping("/select-all/{type}")
    public Result selectAll(@PathVariable Integer type) {
        return cartItemService.selectAll(type);
    }

    @PostMapping("/update-specifications")
    public Result updateSpecifications(@RequestBody UpdateSpecificationsDTO updateSpecificationsDTO) {
        return cartItemService.updateSpecifications(updateSpecificationsDTO);
    }

    @PostMapping("/remove-cartItem-skus")
    public Result removeCartltemBySkus(@RequestBody List<String> skus) {
        return cartItemService.removeCartltemBySkus(skus);
    }

    @DeleteMapping("/cart-item/remove-cartItem/{id}")
    public Result removeCartltemByProductId(@PathVariable Long id){
        return cartItemService.removeCartltemByProductId(id);
    }
}
